/**
 * 天工角色权限工具函数
 */

export interface TiangongUser {
  username: string;
  name: string;
  role?: {
    name: string;
    code: string;
    label: string;
  };
  dataVisibility?: {
    showAdvancedFeatures: boolean;
    showSensitiveData: boolean;
    showDebugInfo: boolean;
    showSystemLogs: boolean;
  };
  featurePermissions?: {
    canCreateApp: boolean;
    canManageWorkflow: boolean;
    canAccessAI: boolean;
    canManageKnowledge: boolean;
    canViewAnalytics: boolean;
    canExportData: boolean;
    canDeleteData: boolean;
    canManageUsers: boolean;
  };
}

/**
 * 检查用户是否为天工角色
 */
export const isTiangongUser = (user: any): boolean => {
  return user?.role?.code === 'tiangong';
};

/**
 * 检查用户是否为访客角色（保持原有功能）
 */
export const isGuestUser = (user: any): boolean => {
  return user?.role?.code === 'guest';
};

/**
 * 检查数据可见性权限
 */
export const canViewData = (
  user: TiangongUser,
  dataType: keyof TiangongUser['dataVisibility'],
): boolean => {
  if (!isTiangongUser(user)) return false;
  return user.dataVisibility?.[dataType] ?? false;
};

/**
 * 检查功能权限
 */
export const canUseFeature = (
  user: TiangongUser,
  feature: keyof TiangongUser['featurePermissions'],
): boolean => {
  if (!isTiangongUser(user)) return false;
  return user.featurePermissions?.[feature] ?? false;
};

/**
 * 获取用户权限摘要
 */
export const getUserPermissionSummary = (user: TiangongUser) => {
  if (!isTiangongUser(user)) {
    return {
      role: '未知角色',
      permissions: [],
      restrictions: [],
    };
  }

  const permissions = [];
  const restrictions = [];

  // 检查功能权限
  if (user.featurePermissions) {
    if (user.featurePermissions.canCreateApp) permissions.push('创建应用');
    if (user.featurePermissions.canManageWorkflow) permissions.push('管理工作流');
    if (user.featurePermissions.canAccessAI) permissions.push('访问AI功能');
    if (user.featurePermissions.canManageKnowledge) permissions.push('管理知识库');
    if (user.featurePermissions.canViewAnalytics) permissions.push('查看分析数据');

    if (!user.featurePermissions.canExportData) restrictions.push('不能导出数据');
    if (!user.featurePermissions.canDeleteData) restrictions.push('不能删除数据');
    if (!user.featurePermissions.canManageUsers) restrictions.push('不能管理用户');
  }

  // 检查数据可见性
  if (user.dataVisibility) {
    if (user.dataVisibility.showAdvancedFeatures) permissions.push('查看高级功能');
    if (!user.dataVisibility.showSensitiveData) restrictions.push('不能查看敏感数据');
    if (!user.dataVisibility.showDebugInfo) restrictions.push('不能查看调试信息');
    if (!user.dataVisibility.showSystemLogs) restrictions.push('不能查看系统日志');
  }

  return {
    role: user.role?.label || '天工',
    permissions,
    restrictions,
  };
};

/**
 * 根据权限过滤菜单项
 */
export const filterMenuByPermissions = (menuItems: any[], user: TiangongUser) => {
  if (!isTiangongUser(user)) return menuItems;

  return menuItems.filter((item) => {
    // 根据功能权限过滤菜单
    switch (item.id) {
      case 'create-app':
        return canUseFeature(user, 'canCreateApp');
      case 'workflow':
        return canUseFeature(user, 'canManageWorkflow');
      case 'ai-features':
        return canUseFeature(user, 'canAccessAI');
      case 'knowledge':
        return canUseFeature(user, 'canManageKnowledge');
      case 'analytics':
        return canUseFeature(user, 'canViewAnalytics');
      default:
        return true;
    }
  });
};

/**
 * 根据权限过滤数据
 */
export const filterDataByVisibility = (data: any[], user: TiangongUser) => {
  if (!isTiangongUser(user)) return data;

  return data.filter((item) => {
    // 根据数据可见性过滤
    if (item.isSensitive && !canViewData(user, 'showSensitiveData')) {
      return false;
    }
    if (item.isDebug && !canViewData(user, 'showDebugInfo')) {
      return false;
    }
    if (item.isSystemLog && !canViewData(user, 'showSystemLogs')) {
      return false;
    }
    return true;
  });
};
